package com.jk.util;

import org.apache.log4j.Logger;

public class NumberUtil {

    private static final Logger logger = Logger.getLogger(NumberUtil.class);

    public static Double toDouble(Object o) {
        if (o == null) {
            return 0.0;
        }
        String s = String.valueOf(o);
        if (StringUtil.isEmpty(s)) {
            return 0.0;
        }
        try {
            return Double.valueOf(s);
        } catch (NumberFormatException e) {
            logger.error("数字转化错误", e);
        }
        return 0.0;
    }

    public static Long toLong(Object o) {
        if (o == null) {
            return 0L;
        }
        String s = String.valueOf(o);
        if (StringUtil.isEmpty(s)) {
            return 0L;
        }
        try {
            int index = s.indexOf(".");
            index = index > -1 ? index : s.length();
            return Long.parseLong(s.substring(0, index));
        } catch (NumberFormatException e) {
            logger.error("数字转化错误", e);
        }
        return 0L;
    }

    public static Integer toInt(Object o) {
        if (o == null) {
            return 0;
        }
        String s = String.valueOf(o);
        if (StringUtil.isEmpty(s)) {
            return 0;
        }
        try {
            int index = s.indexOf(".");
            index = index > -1 ? index : s.length();
            return Integer.parseInt(s.substring(0, index));
        } catch (NumberFormatException e) {
            logger.error("数字转化错误", e);
        }
        return 0;
    }

    public static boolean equals(Long s, Long d) {
        if (s == null) {
            s = 0L;
        }
        if (d == null) {
            d = 0L;
        }
        return s.equals(d);
    }
}
